using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using MercuryParticleEngine.Emitters;

namespace BumbleGame
{
    class Bumblepig //: AnimatedTexture //probably doesn't need to inherit animated texture any more
    {
        
        public int score, coins, life, diveShadow;
        private int screenWidth, screenHeight;
        private float emitterHypotenuse, emitterTheta, invulnTimer, fExplodingBulletTimer;
        public bool isExplosionVisible;
        public bool dying, dead, newPig, onTopLayer, bulletDestroyed, invulnerable;
        public Vector2 spritePosition;
        public float spriteRotation, spriteScale, floatcoins;
        public AnimatedTexture BumbleBody, Bullet, ExplodedBullet;
        private AnimatedTexture BumbleWings, BumbleShadow, NoseBall;
        

        public Bumblepig()
        {
            //distance from center of bumblepig to the emitter position
            emitterHypotenuse = 38.2f;
            //value of the angle in radians used to determine the direction of the hypotenuse
            emitterTheta = 0.748f;
            score = 0;
            coins = 0;
            life = 6;
            floatcoins = 0;
            invulnTimer = 0.0f;
            invulnerable = false;
            dying = false;
            dead = false;
            newPig = false;
            onTopLayer = true;
            bulletDestroyed = true;
            diveShadow = 113;
            BumbleBody = new AnimatedTexture();
            BumbleShadow = new AnimatedTexture();
            BumbleWings = new AnimatedTexture();
            Bullet = new AnimatedTexture();
            ExplodedBullet = new AnimatedTexture();
            NoseBall = new AnimatedTexture();
        }
        public void LoadPig(Viewport viewport, ContentManager content, GraphicsDeviceManager graphics)
        {
            screenWidth = viewport.Width;
            screenHeight = viewport.Height;

            BumbleBody.Load(graphics.GraphicsDevice, content, "Images/bp_avatar_yellow_5of8", 8, 1, 8, 0, 4, true);
            BumbleWings.Load(graphics.GraphicsDevice, content, "Images/bp_avatar_wing_5of8", 8, 1, 60, 0, 4, true);
            BumbleShadow.Load(graphics.GraphicsDevice, content, "Images/bp_avatar_shadowstrip", 8, 1, 8, 0, 7, false);
            Bullet.Load(graphics.GraphicsDevice, content, "Images/pollen_dummy", 1, 1, 1, 0, 0, false);
            ExplodedBullet.Load(graphics.GraphicsDevice, content, "Images/speckels", 1, 1, 1, 0, 0, false);
            NoseBall.Load(graphics.GraphicsDevice, content, "Images/pollen_dummy", 1, 1, 1, 0, 0, false);

            spritePosition = new Vector2(viewport.Width / 2, viewport.Height / 2);
            spriteRotation = BumbleBody.SpriteRotation;
            spriteScale = BumbleBody.SpriteScale;

            NoseBall.SpritePosition.X = spritePosition.X;
            NoseBall.SpritePosition.Y = spritePosition.Y - 35;
            Bullet.SpritePosition = NoseBall.SpritePosition;
            Bullet.SpriteScale = 0.5f;
           
            BumbleBody.SpritePosition = spritePosition;
            BumbleWings.SpritePosition = spritePosition;
            BumbleShadow.SpritePosition.X = spritePosition.X + 6;
            BumbleShadow.SpritePosition.Y = spritePosition.Y + diveShadow;

            BumbleWings.SpriteRotation = spriteRotation;
            BumbleShadow.SpriteRotation = spriteRotation;

            BumbleWings.SpriteScale = spriteScale;           

            BumbleShadow.tint = Color.Black;
            BumbleShadow.SpriteScale = 0.4f;

            BumbleBody.SetHitBoxOffset(50);

        }
        public void UpdateBumblepig(float elapsed, int nConfidenceMode, AnimatedTexture statusPig)
        {
            BumbleBody.UpdateFrame(elapsed);
            BumbleWings.UpdateFrame(elapsed);
            BumbleShadow.UpdateFrame(elapsed);
            ExplodedBullet.UpdateFrame(elapsed);

            if (invulnerable)
            {
                invulnTimer += elapsed;
                BumbleBody.tint = Color.Red;
                statusPig.firstFrame = 1;
                statusPig.lastFrame = 1;
            }
            //Change the status pig to ouch frame quicker than the invulnerability wears off
            if (invulnTimer > 1.0f)
            {
                statusPig.firstFrame = 0;
                statusPig.lastFrame = 0;
            }

            //make bumblepig normal again 2 seconds after being hit
            if (invulnTimer > 2.0)
            {
                invulnerable = false;
                invulnTimer = 0.0f;
                BumbleBody.tint = Color.White;
            }

            if (spriteScale < 1)
            {
                onTopLayer = false;
            }

            if (life == 0)
            {
                dying = true;
            }
            if (dying && !dead)
            {
                spriteScale -= 0.02f;
                spriteRotation += 0.15f;
                if (spriteScale < 0.02f)
                {
                    spriteScale = 0;
                    dead = true;
                }
            }
            if (newPig)
            {
                life = 6;
                spriteScale = 1f;
                spriteRotation = 0;
                spritePosition.X = screenWidth / 2;
                spritePosition.Y = screenHeight / 2;
                dying = false;
                dead = false;
                newPig = false;
            }

            BumbleBody.SpritePosition = spritePosition;
            BumbleWings.SpritePosition = spritePosition;
            BumbleShadow.SpritePosition.X = spritePosition.X + 6;
            BumbleShadow.SpritePosition.Y = spritePosition.Y + diveShadow;

            BumbleBody.SpriteRotation = spriteRotation;
            BumbleWings.SpriteRotation = spriteRotation;
            BumbleShadow.SpriteRotation = spriteRotation;

            BumbleBody.SpriteScale = spriteScale;
            BumbleWings.SpriteScale = spriteScale;
            BumbleShadow.SpriteScale = 0.4f;
            if (nConfidenceMode > 0)
            {
                NoseBall.UpdateFrame(elapsed);
                Bullet.UpdateFrame(elapsed);
                
                NoseBall.SpritePosition.X = spritePosition.X + (40 * (float)Math.Sin(spriteRotation));
                NoseBall.SpritePosition.Y = spritePosition.Y - (40 * (float)Math.Cos(spriteRotation));
                
                NoseBall.SpriteScale = spriteScale;
                if (bulletDestroyed)
                { Bullet.SpritePosition = NoseBall.SpritePosition; }
                else
                { 
                    Bullet.SpritePosition.Y -= (8 * (float)Math.Cos(Bullet.SpriteRotation));
                    Bullet.SpritePosition.X += (8 * (float)Math.Sin(Bullet.SpriteRotation));
                }
            }
            if (Bullet.SpritePosition.Y < (0 - Bullet.aTexture.Height) ||
                Bullet.SpritePosition.X < (0 - Bullet.aTexture.Width) ||
                Bullet.SpritePosition.X > (1024 + Bullet.aTexture.Width))
            { bulletDestroyed = true; }

            if (isExplosionVisible)
            {               
                fExplodingBulletTimer += elapsed;
                if (fExplodingBulletTimer > 1.0f)
                {
                    isExplosionVisible = false;
                    fExplodingBulletTimer = 0.0f;
                }
            }

            if (dying)
            {
                BumbleShadow.SpriteScale = spriteScale / 2;
            }

            if (coins > 0) floatcoins = (float)coins / 100;
        }

        public void DrawPigShadow(SpriteBatch batch)
        {
            BumbleShadow.DrawFrame(batch);
        }
        
        public void DrawPig(SpriteBatch batch, int nConfidenceMode)
        {
            BumbleBody.DrawFrame(batch);
            BumbleWings.DrawFrame(batch);
            if (nConfidenceMode > 0)
            {
                NoseBall.DrawFrame(batch);
                if (!bulletDestroyed) Bullet.DrawFrame(batch);
                if (isExplosionVisible) ExplodedBullet.DrawFrame(batch);
            }
        }

        public void RotationCorrection(Emitter emitterLeft, Emitter emitterRight, Emitter emitterLeftConstant, Emitter emitterRightConstant)
        {

            emitterLeft.Position.X = spritePosition.X - (emitterHypotenuse * (float)Math.Sin(spriteRotation + emitterTheta));
            emitterLeft.Position.Y = spritePosition.Y + (emitterHypotenuse * (float)Math.Cos(spriteRotation + emitterTheta));

            emitterRight.Position.X = spritePosition.X + (emitterHypotenuse * (float)Math.Cos(spriteRotation + (MathHelper.PiOver2 - emitterTheta)));
            emitterRight.Position.Y = spritePosition.Y + (emitterHypotenuse * (float)Math.Sin(spriteRotation + (MathHelper.PiOver2 - emitterTheta)));

            emitterLeftConstant.Position = emitterLeft.Position;
            emitterRightConstant.Position = emitterRight.Position;

            if (spriteRotation < 0.1 && spriteRotation > -0.1) spriteRotation = 0;
            
            
        }

        public void HurtBumblepig(SoundBank sfxBank)
        {
            if (invulnerable || dead) return;
            life -= 1; //if bug hits pig, damage them both
            //make bumblepig invulnerable for 2 secs after getting hit
            invulnerable = true;
            sfxBank.PlayCue("BumblepigHurt");
        }

        public void ExplodeBullet()
        {
            if (dead) return;
            ExplodedBullet.SpritePosition = Bullet.SpritePosition;
            bulletDestroyed = true;
            isExplosionVisible = true;
        }
    }
}
